package com.luxx.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.util.*;

/**
 * Excel工具类
 *
 * @author luxx
 */
public class ExcelUtil {
    private final static Map<Class, Object> clazzParamMap = new HashMap<>();

    static {
        clazzParamMap.put(Date.class, FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss" ));
        Map bool = new HashMap();
        bool.put(true, "是" );
        bool.put(false, "否" );
        clazzParamMap.put(boolean.class, bool);
    }


    /**
     * @param records 数据记录
     * @param headers 字段表头映射  {"title":"标题"}
     * @return HSSFWorkbook 对象，可写入到文件或者输出流
     */
    public static HSSFWorkbook createExcel(List<JSONObject> records, LinkedHashMap<String, String> headers) {
        HSSFWorkbook wb = new HSSFWorkbook();               //创建一个Excel文件
        HSSFSheet sheet = wb.createSheet("sheet1" );         //创建一个工作簿sheet
        sheet.setDefaultColumnWidth(20);                    //设置表格默认列宽度为15个字节

        HSSFCellStyle style = wb.createCellStyle();         //创建一个剧中格式
        style.setAlignment(HorizontalAlignment.CENTER);

        HSSFRow row = sheet.createRow(0);                //创建表头
        int i = 0;
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            HSSFCell cell = row.createCell(i++);
            cell.setCellStyle(style);
            HSSFRichTextString text = new HSSFRichTextString(entry.getValue());
            cell.setCellValue(text);
        }
        i = 1;
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss" );
        for (JSONObject record : records) {
            row = sheet.createRow(i++);
            int j = 0;
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                Object value = record.get(entry.getKey());
                if (value instanceof String) {
                    row.createCell(j).setCellValue((String) value);
                } else if (value instanceof Date) {
                    row.createCell(j).setCellValue(format.format(value));
                } else {
                    row.createCell(j).setCellValue(value.toString());
                }
                j++;
            }
        }
        return wb;
    }

    /**
     * @param records 数据记录
     * @param headers 字段表头映射  {"title":"标题"}
     * @param file    目标文件
     * @throws IOException
     */
    public static void writeToFile(List<JSONObject> records, LinkedHashMap<String, String> headers, File file) throws IOException {
        if (file.exists()) {
            throw new FileAlreadyExistsException("文件已经存在" );
        }
        HSSFWorkbook wb = ExcelUtil.createExcel(records, headers);
        FileOutputStream fout = new FileOutputStream(file);
        wb.write(fout);
        fout.close();
    }
}